package com.liuwei.spring.cloud.junit.leetcode.interview;

public class GasStation {

    /**
     * https://leetcode.cn/problems/gas-station/?envType=study-plan-v2&envId=top-interview-150
     * @param args
     */
    public static void main(String[] args) {
        GasStation solution = new GasStation();
        int[] gas = new int[]{2,3,4};
        int[] cost = new int[]{3,4,3};
        int result = solution.canCompleteCircuit(gas,cost);
        System.out.println(result);
    }

    public int canCompleteCircuit(int[] gas, int[] cost) {
        for (int i = 0; i < gas.length; i++) {
            int oil = gas[i];
            boolean success = true;
            for (int j = 0 ; j < gas.length; j++) {
                oil = oil - cost[(i+j)%gas.length];
                if(oil<0){
                    success = false;
                    break;
                }else{
                    oil = oil+gas[(i+j+1)%gas.length];
                }
            }
            if(success){
                return i;
            }
        }
        return -1;
    }
}
